package com.rongc.client.freight.business.mine.view.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.LogUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.UniApplication;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.view.widget.DividerItemDecoration;
import com.rongc.client.freight.business.mine.model.ScoreBean;
import com.rongc.client.freight.business.mine.request.api.MineScoreApi;
import com.rongc.client.freight.business.mine.view.adapter.RecyclerScoreAdapter;
import com.rongc.client.freight.utils.UtilMethod;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by ccqx on 2017/7/17.
 */

public class MineScoreActivity extends BaseActivity {
    @Bind(R.id.tv_balance)
    TextView mTvBalance;
    @Bind(R.id.recycler)
    RecyclerView mRecyclerView;
    RecyclerScoreAdapter modelAdapter;

    List<ScoreBean> datas;

    Response.Listener<ScoreBean[]> respOrderListener = new Response.Listener<ScoreBean[]>() {

        @Override
        public void onResponse(ScoreBean[] response) {
            if (HandleCode.requestSuccess()) {
                datas.clear();
                if(response != null)
                    for (int i=0;i<response.length;i++){
                        datas.add(response[i]);
                        modelAdapter.notifyDataSetChanged();
                    } else {
                    UtilMethod.dismissProgressDialog(MineScoreActivity.this);
                }
            }
        }};


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            LogUtils.i("error.getMessage(): " + error.getMessage());
            UtilMethod.dismissProgressDialog(MineScoreActivity.this);
        }
    };

    @Override
    public void onResume() {
        super.onResume();
        RequestManager.getInstance().call(new MineScoreApi(
                new MineScoreApi.MineScoreParams(),
                respOrderListener, errorListener));
    }


    @Override
    public void initView() {
        initToolbar(R.string.score_title);

        datas = new ArrayList<>();
        modelAdapter = new RecyclerScoreAdapter(this,datas);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        mRecyclerView.setAdapter(modelAdapter);
        mTvBalance.setText(UniApplication.getInstance().getUserInfo().getIntegral());


    }


    @Override
    public int getLayoutId() {
        return R.layout.activity_score_host;
    }
}
